package com.ac.scum.log.web.model.server.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.result.Result;
import com.ac.scum.log.bean.Package;
import com.ac.scum.log.bean.Server;
import com.ac.scum.log.web.model.server.service.ServerService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

/**
 * 服务器
 */
@RestController
@RequestMapping("server")
@AllArgsConstructor
public class ServerController {
    private final ServerService serverService;

    @GetMapping("by{userId}")
    public Result<List<Server>> getServersByUserId(@PathVariable Long userId) {
        if (userId.equals(1765425375730122753L)) {
            return Result.ok(serverService.getBaseMapper().selectList(Wrappers.emptyWrapper()));
        }
        return Result.ok(serverService.getServersByUserId(userId));
    }

    @GetMapping("packages")
    public Result<List<Package>> getCurrentServerPackages() throws BaseException {
        return Result.ok(serverService.getServerPackages(CurrentUserUtil.getCurrentServerId()));
    }

    @PostMapping("use")
    public Result<Boolean> loginUseServer(@RequestParam("serverId") Long serverId) throws BaseException {
        return Result.ok(serverService.loginUserUseServer(serverId));
    }

    @PostMapping("insert")
    public Result<Server> insert(@RequestBody Server server) throws BaseException {
        serverService.save(server, CurrentUserUtil.getCurrentLogUser().getId());
        return Result.ok(server);
    }

    @PostMapping("active")
    public Result<Boolean> activePackage(@RequestParam("serverId") Long serverId, @RequestParam("keyCard") String keyCard) throws BaseException {
        return Result.ok(serverService.active(serverId, keyCard));
    }

    @GetMapping("proxyQuery")
    public Result<String> queryProxy(@Param("key") String key) throws IOException, InterruptedException {
        HttpResponse<String> response = HttpClient.newHttpClient().send(HttpRequest.newBuilder()
                .uri(URI.create("https://api.battlemetrics.com/servers?fields[server]=rank,name,players,maxPlayers,address,ip,port,country,status&relations[server]=game,serverGroup&filter[game]=scum&filter[search]=" + key))
                .header("Content-Type", "application/json") // 设置请求头，这里假设发送JSON数据
                .GET().build(), HttpResponse.BodyHandlers.ofString());
        return Result.ok(response.body());
    }

    @GetMapping("getHasAlarmServers/by{name}")
    public Result<List<Server>> getHasAlarmServersByName(@PathVariable String name) {
        return Result.ok(serverService.getHasAlarmServersByName(name));
    }

    @GetMapping("info")
    public Result<Server> getServerInfo() throws BaseException {
        return Result.ok(serverService.getById(CurrentUserUtil.getCurrentServerId()));
    }

    @PostMapping("update")
    public Result<Server> updateServer(@RequestBody Server server) throws BaseException {
        server.setId(CurrentUserUtil.getCurrentServerId());
        server.setApiId(null);
        serverService.updateById(server);
        return Result.ok(server);
    }
}
